//
//  KeyboardHandleViewController.m
//  VoiceMemo
//
//  Created by shaowei on 6/24/14.
//  Copyright (c) 2014 shede333. All rights reserved.
//

#import "KeyboardShowHideHandler.h"
#import "UIView+sizes.h"

typedef NS_ENUM(NSInteger, KeyboardActionType) {
    KeyboardActionTypeOfShow = 1,
    KeyboardActionTypeOfHide,
};

@interface KeyboardHandleWidget()


@end

@implementation KeyboardHandleWidget


@end


/**************************************************************************************************/

@interface KeyboardNotifyModel : NSObject

@property (nonatomic, assign) CGRect frameOfBegin;
@property (nonatomic, assign) CGRect frameOfEnd;
@property (nonatomic, assign) CGFloat animationDuration;
@property (nonatomic, assign) UIViewAnimationOptions animationOptions;

- (NSString *)description;

@end

@implementation KeyboardNotifyModel

- (NSString *)description{
    return [NSString stringWithFormat:@"frameOfBegin:%@, frameOfEnd:%@, animationDuration:%f, animationOptions:%X",
            NSStringFromCGRect(self.frameOfBegin),
            NSStringFromCGRect(self.frameOfEnd),
            self.animationDuration,
            self.animationOptions];
}

@end

/**************************************************************************************************/

@interface KeyboardShowHideHandler ()

@property (nonatomic, strong) NSMutableArray *arrOfWidget;
@property (nonatomic, assign) BOOL isNotifying;

@end

@implementation KeyboardShowHideHandler

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.arrOfWidget = [NSMutableArray arrayWithCapacity:5];
//        [self startKeyboardNotify];
    }
    return self;
}

- (void)dealloc
{
    [self stopKeyboardNotify];
}

#pragma mark - Function - Public
- (BOOL)addWidget:(KeyboardHandleWidget *)widgetModel{
    if ([widgetModel isKindOfClass:[KeyboardHandleWidget class]]) {
        [self.arrOfWidget addObject:widgetModel];
        return YES;
    }else{
        return NO;
    }
    
}

- (NSArray *)getAllWidget{
    return _arrOfWidget;
}

- (void)startKeyboardNotify{
    
    if (self.isNotifying) {
        return;
//        [self stopKeyboardNotify]; //防止重复注册通知
    }
    
    self.isNotifying = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

- (void)stopKeyboardNotify{
    if (!self.isNotifying) {
        return;  //防止重复 停止通知
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
//    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    self.isNotifying = NO;
}

- (void)resetData{
    [self.arrOfWidget removeAllObjects];
}

#pragma mark - Function - Private

- (KeyboardNotifyModel *)convertToModelFromNotify:(NSNotification *)notification{
    NSDictionary* info = [notification userInfo];
    KeyboardNotifyModel *model = [[KeyboardNotifyModel alloc] init];
    model.frameOfBegin = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    model.frameOfEnd = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    model.animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    model.animationOptions = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
    return model;
}

#pragma mark - Notification

- (void)_keyboardWillShow:(NSNotification *)notification{
    KeyboardNotifyModel *model = [self convertToModelFromNotify:notification];
//    DVLog(@"%@",[notification userInfo]);
    
    [self batchHandleKbActionType:KeyboardActionTypeOfShow notifyModel:model];
}

- (void)_keyboardWillHide:(NSNotification *)notification{
    KeyboardNotifyModel *model = [self convertToModelFromNotify:notification];
//    DVLog(@"%@",[notification userInfo]);

    [self batchHandleKbActionType:KeyboardActionTypeOfHide notifyModel:model];
}

- (void)_keyboardWillChange:(NSNotification *)notification{
//    KeyboardNotifyModel *model = [self convertToModelFromNotify:notification];
//    DVLog(@"%@",[notification userInfo]);
}

#pragma mark - widget handle

- (void)batchHandleKbActionType:(KeyboardActionType)actionType notifyModel:(KeyboardNotifyModel *)model{
    [UIView animateWithDuration:model.animationDuration
                          delay:0
                        options:model.animationOptions
                     animations:^{
                         for (KeyboardHandleWidget *widget in self.arrOfWidget) {
                             [self handleWidget:widget
                                   kbActionType:actionType
                                    notifyModel:model];
                         }
                     } completion:^(BOOL finished) {
                         
                     }];
}

- (void)handleWidget:(KeyboardHandleWidget *)widget kbActionType:(KeyboardActionType)actionType notifyModel:(KeyboardNotifyModel *)model {
    switch (actionType) {
        case KeyboardActionTypeOfHide:
        {
            widget.widgetView.frame = widget.frameOfKbHide;
//            NSLog(@"kbHandle-show-frame:%@,contentSize:%@",NSStringFromCGRect(widget.frameOfKbHide),
//                 NSStringFromCGSize([(UIScrollView *)widget.widgetView contentSize]));
        }
            break;
        case KeyboardActionTypeOfShow:
        {
            CGRect rectInRoot = widget.frameOfKbHide;
//            widgetBounds.origin = widget.widgetView.bounds.origin;//防止ScrollView漂移
////            NSLog(@"bounds:%@", NSStringFromCGRect(widget.widgetView.bounds));
//            CGRect rectInRoot = [widget.widgetRootView convertRect:widgetBounds fromView:widget.widgetView];
            CGFloat heightOfwidgetToBottom = widget.widgetRootView.height - rectInRoot.origin.y - rectInRoot.size.height;
            
            CGFloat kbHeightOfKb= model.frameOfEnd.size.height + widget.intervalOfWidgetToKbWhenKbShow;
            CGFloat cutDownHeight = kbHeightOfKb - heightOfwidgetToBottom;
            CGFloat widgetHeight = widget.frameOfKbHide.size.height - cutDownHeight;
            widgetHeight = (widgetHeight > 0)?widgetHeight:0;
            
            CGRect rectOfKeyShow = rectInRoot;
            rectOfKeyShow.size.height = widgetHeight;
            
            widget.widgetView.frame = rectOfKeyShow;
//            NSLog(@"kbHandle-show-frame:%@,contentSize:%@",NSStringFromCGRect(rectOfKeyShow),NSStringFromCGSize([(UIScrollView *)widget.widgetView contentSize]));
        }
            break;
        default:
            break;
    }
}





@end
